<?php

namespace app\data\controller\total;

use app\data\service\CarService;
use app\data\service\CityUserRelationService;
use think\admin\Controller;
use think\Db;

/**
 * 首页
 * Class Portal
 * @package app\data\controller\total
 */
class Portal extends Controller
{
    /**
     * 首页
     * @auth true
     * @menu true
     */
    public function index()
    {
        $todayMinDate = date('Y-m-d 00:00:00');
        $todayMaxDate = date('Y-m-d 23:59:59');

        // 新增派单数量
        $this->todayRecordCount = $this->app->db->name('data_record')
            ->where('time_docking', '>=', $todayMinDate)
            ->where('time_docking', '<=', $todayMaxDate)
            ->count();


        // 今日完工数量
        $this->todayCompleteCount = $this->app->db->name('data_record')
            ->where('time_complete', '>=', $todayMinDate)
            ->where('time_complete', '<=', $todayMaxDate)
            ->count();

        // 获取城市师傅树
        $cityRelations = CityUserRelationService::instance()->getCityUserTree();

        // 当日有效人力
        $checkInUsers = CityUserRelationService::instance()->checkInUsers($cityRelations);
        $this->checkInUserCount = count($checkInUsers);

        // 次日流失数量 = 派单数-产出
//        $yesterday = date("Y-m-d",strtotime("-1 day"));
//        $yesterdayReport = $this->app->db->name('report_daily_output')
//            ->where('date', $yesterday)
//            ->findOrEmpty();
//        if ($yesterdayReport) {
//            $this->yesterdayLossCount = $yesterdayReport['record_count'] - $yesterdayReport['complete_today_count'];
//        } else {
//            $this->yesterdayLossCount = 0;
//        }

        // 未完成喷漆，超过30分钟倒计时数据
        $this->notReplay30 = 0;
        $todayRecords = $this->app->db->name('data_record')->whereIn('paint_status', [0, -1])->where('time_docking', '>=', $todayMinDate)->select()->toArray();
        foreach ($todayRecords as $todayRecord) {
            if (empty($todayRecord['time_invite']) && (time() > (strtotime($todayRecord['time_docking']) + 1800))) {
                $this->notReplay30 ++;
            }
        }

        // 累计派单数量
        $this->totalRecordCount = $this->app->db->name('data_record')->count();

        // 累计完工数量
        $this->totalCompleteCount = $this->app->db->name('data_record')->where('paint_status', 1)->count();

        // 累计待施工数量
        $this->totalNotCompleteCount = $this->app->db->name('data_record')->whereIn('paint_status', [0, -1])->count();

        // 累计流失数量
//        $this->totalLossCount = $this->app->db->name('report_daily_output')->sum('record_count') - $this->app->db->name('report_daily_output')->sum('complete_today_count');

        // 当月结单数量
        $this->monthCompleteNum = $this->app->db->name('data_record')->where('paint_status', 1)->where('time_complete', '>=', date('Y-m-0 00:00:00'))->count();

        // 今日城市订单数量
//        $cityRecordNum = $this->app->db->name('data_record')
//            ->group('city')
//            ->field('city,time_docking,COUNT(*) as count')
//            ->where('time_docking', '>=', $todayMinDate)
//            ->where('time_docking', '<=', $todayMaxDate)
//            ->select()->toArray();

        // 预警城市
        $this->earlyWarningCities = CityUserRelationService::instance()->earlyWarningCities($todayMinDate, $todayMaxDate);

        // 柱状图-城市完工统计
        $this->yesterday = date('Y-m-d', strtotime("-1days"));
        $this->cities = $this->_cities();

        $this->fetch();
    }

    /**
     * 城市完工统计-ajax请求
     * @return void
     */
    public function cities()
    {
        $this->success('', $this->_cities());
    }

    /**
     * 城市完工统计
     * @return array
     */
    private function _cities()
    {
        $date = $this->request->request('date', date('Y-m-d', strtotime("-1days")));

        // 城市完工统计
        foreach ($this->app->db->name('report_daily_output')
                     ->field('date,from_city,group_energy,record_count,complete_count,complete_today_count')
                     ->where('date', $date)
                     ->group('from_city')->cursor() as $vo) {
            $response[] = [
                '城市' => $vo['from_city'],
                '去化能力' => $vo['group_energy'],
                '派单数量' => $vo['record_count'],
                '完工数量' => $vo['complete_count'],
                '当日派单完工数量' => $vo['complete_today_count'],
            ];
        }

        if (empty($response)) {
            $response[] = [
                '城市' => '',
                '城市订单金额' => 0,
            ];
        }

        return $response;
    }
}